<template>
  <el-dialog
    :title="!dataForm.groupbuyId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="经纪人id" prop="agentId">
      <el-input v-model="dataForm.agentId" placeholder="经纪人id"></el-input>
    </el-form-item>
    <el-form-item label="封面图片路径" prop="groupbuyImg">
      <el-input v-model="dataForm.groupbuyImg" placeholder="封面图片路径"></el-input>
    </el-form-item>
    <el-form-item label="团购标题" prop="groupbuyTitle">
      <el-input v-model="dataForm.groupbuyTitle" placeholder="团购标题"></el-input>
    </el-form-item>
    <el-form-item label="楼盘地址" prop="groupbuyAddress">
      <el-input v-model="dataForm.groupbuyAddress" placeholder="楼盘地址"></el-input>
    </el-form-item>
    <el-form-item label="报名人数" prop="groupbuyNum">
      <el-input v-model="dataForm.groupbuyNum" placeholder="报名人数"></el-input>
    </el-form-item>
    <el-form-item label="开始日期" prop="groupbuyStartdate">
      <el-input v-model="dataForm.groupbuyStartdate" placeholder="开始日期"></el-input>
    </el-form-item>
    <el-form-item label="结束日期" prop="groupbuyEnddate">
      <el-input v-model="dataForm.groupbuyEnddate" placeholder="结束日期"></el-input>
    </el-form-item>
    <el-form-item label="介绍内容" prop="groupbuyIntroduce">
      <el-input v-model="dataForm.groupbuyIntroduce" placeholder="介绍内容"></el-input>
    </el-form-item>
    <el-form-item label="是否开团状态0开团1未开团" prop="groupbuyWhetherStatus">
      <el-input v-model="dataForm.groupbuyWhetherStatus" placeholder="是否开团状态0开团1未开团"></el-input>
    </el-form-item>
    <el-form-item label="是否展示状态0展示1不展示" prop="groupbuyShowStatus">
      <el-input v-model="dataForm.groupbuyShowStatus" placeholder="是否展示状态0展示1不展示"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          groupbuyId: 0,
          agentId: '',
          groupbuyImg: '',
          groupbuyTitle: '',
          groupbuyAddress: '',
          groupbuyNum: '',
          groupbuyStartdate: '',
          groupbuyEnddate: '',
          groupbuyIntroduce: '',
          groupbuyWhetherStatus: '',
          groupbuyShowStatus: ''
        },
        dataRule: {
          agentId: [
            { required: true, message: '经纪人id不能为空', trigger: 'blur' }
          ],
          groupbuyImg: [
            { required: true, message: '封面图片路径不能为空', trigger: 'blur' }
          ],
          groupbuyTitle: [
            { required: true, message: '团购标题不能为空', trigger: 'blur' }
          ],
          groupbuyAddress: [
            { required: true, message: '楼盘地址不能为空', trigger: 'blur' }
          ],
          groupbuyNum: [
            { required: true, message: '报名人数不能为空', trigger: 'blur' }
          ],
          groupbuyStartdate: [
            { required: true, message: '开始日期不能为空', trigger: 'blur' }
          ],
          groupbuyEnddate: [
            { required: true, message: '结束日期不能为空', trigger: 'blur' }
          ],
          groupbuyIntroduce: [
            { required: true, message: '介绍内容不能为空', trigger: 'blur' }
          ],
          groupbuyWhetherStatus: [
            { required: true, message: '是否开团状态0开团1未开团不能为空', trigger: 'blur' }
          ],
          groupbuyShowStatus: [
            { required: true, message: '是否展示状态0展示1不展示不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.groupbuyId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.groupbuyId) {
            this.$http({
              url: this.$http.adornUrl(`/agent/groupbuy/info/${this.dataForm.groupbuyId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.agentId = data.groupbuy.agentId
                this.dataForm.groupbuyImg = data.groupbuy.groupbuyImg
                this.dataForm.groupbuyTitle = data.groupbuy.groupbuyTitle
                this.dataForm.groupbuyAddress = data.groupbuy.groupbuyAddress
                this.dataForm.groupbuyNum = data.groupbuy.groupbuyNum
                this.dataForm.groupbuyStartdate = data.groupbuy.groupbuyStartdate
                this.dataForm.groupbuyEnddate = data.groupbuy.groupbuyEnddate
                this.dataForm.groupbuyIntroduce = data.groupbuy.groupbuyIntroduce
                this.dataForm.groupbuyWhetherStatus = data.groupbuy.groupbuyWhetherStatus
                this.dataForm.groupbuyShowStatus = data.groupbuy.groupbuyShowStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/agent/groupbuy/${!this.dataForm.groupbuyId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'groupbuyId': this.dataForm.groupbuyId || undefined,
                'agentId': this.dataForm.agentId,
                'groupbuyImg': this.dataForm.groupbuyImg,
                'groupbuyTitle': this.dataForm.groupbuyTitle,
                'groupbuyAddress': this.dataForm.groupbuyAddress,
                'groupbuyNum': this.dataForm.groupbuyNum,
                'groupbuyStartdate': this.dataForm.groupbuyStartdate,
                'groupbuyEnddate': this.dataForm.groupbuyEnddate,
                'groupbuyIntroduce': this.dataForm.groupbuyIntroduce,
                'groupbuyWhetherStatus': this.dataForm.groupbuyWhetherStatus,
                'groupbuyShowStatus': this.dataForm.groupbuyShowStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
